<template>
  <div class="patientList">
    <div class="wrap">
      <div style="max-height: 80vh; overflow: auto">
        <van-cell-group>
          <van-cell
            @click="enter(i)"
            v-for="(i, index) in List"
            :key="index"
            :title="i.name"
            is-link
          />
        </van-cell-group>
      </div>
      <!-- <div class="bottom" style="">
        如以上没有您想要的就诊人，可先去<span style="color: #40bb90">就诊人管理</span
        >，进行就诊人添加。
      </div> -->
    </div>
  </div>
</template>

<script lang="ts" setup>
import { IqueryBindPatInfo } from '@/typings/queryBindPatInfo';
import { commonTypes } from '@/typings/commonTypes';
import { queryBindPatInfo, queryNoticeSerialNo } from '@/services/inHospitalApi';
import { useTo } from '@/utils/useTo';
import { useStore } from 'vuex';
const store = useStore<commonTypes.IState>();

import { Cell, CellGroup, Dialog, Toast } from 'vant';

//因为这里用了script的setup方式就用不到 defineComponent 这个定义组件的api了
import { useSlots, useAttrs, defineProps, onMounted, watchEffect, ref } from 'vue';
// 跳路由用
import router from '@/router/index';
// 获取路由参数
import { useRoute } from 'vue-router';
const route = useRoute();
// const slots = useSlots();
// const attrs = useAttrs();

//import HelloWorld from '@/components/HelloWorld.vue'; // @ is an alias to /src

const List = ref<IqueryBindPatInfo.IBusObj[]>([]);

const tk = () => {
  Dialog.alert({
    message: '没有查询到符合预约条件的入院通知单！',
    confirmButtonColor: '#40BB90',
  }).then(() => {
    // on close
  });
};

//查询绑定的就诊人信息
const queryBindPatInfoFn = async () => {
  const toast = Toast.loading({
    overlay: true,
    duration: 0,
  });
  let pam = {
    userId: route.query.userId ?? null,
    hospitalId: store.state.hospitalId,
  };
  const [err, res] = await useTo(queryBindPatInfo(pam));
  toast.clear();
  if (err) return Toast(err.errorMsg);
  List.value = res.busObj;
  console.log('就诊人列表:', List.value);
};

//查询医院入院通知书序列号
const queryNoticeSerialNoFn = async (pam: any) => {
  const toast = Toast.loading({
    overlay: true,
    duration: 0,
  });
  /*
  hospitalUUID	是	string	医院的唯一标识UUID
  patCardNo	是	string	就诊人的就诊卡号
  idNo	是	string	身份证号
  */
  const [err, res] = await useTo(queryNoticeSerialNo(pam));
  toast.clear();
  if (err) return Toast(err.errorMsg);
  if (res.busObj) {
    router.push({
      path: 'continue',
      query: { noticeSerialNo: res.busObj, userId: route.query.userId ?? null },
    });
  } else {
    tk();
  }
  console.log('序列号:', res.busObj);
};

//进入
const enter = (obj: IqueryBindPatInfo.IBusObj) => {
  console.log(obj);
  //let pam = { hospitalUUID: 4, patCardNo: 2, idNo: '2225144' };
  let cardNo = obj.cardList && obj.cardList.length && obj.cardList[0].cardNo;
  let pam = { hospitalUUID: 4, patCardNo: cardNo ?? null, idNo: obj.idNo };
  store.dispatch('currentUserAction', obj);
  queryNoticeSerialNoFn(pam);
};

onMounted(() => {
  //console.log('onMounted:', process.env.NODE_ENV);
  queryBindPatInfoFn();
});

//watchEffect 会最先执行
watchEffect(() => {
  //console.log('watchEffect:', 3333);
});
</script>

<style scoped lang="scss">
.patientList {
  .wrap {
    padding-top: 8px;
    .bottom {
      font-size: 14px;
      padding: 16px;
      color: #999999;
    }
  }
}
</style>
